package com.senlingspace.web.controller.headquarters;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.senlingspace.common.annotation.Log;
import com.senlingspace.common.core.controller.BaseController;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.core.page.TableDataInfo;
import com.senlingspace.common.enums.BusinessType;
import com.senlingspace.common.utils.poi.ExcelUtil;
import com.senlingspace.system.domain.SlkjOrder;
import com.senlingspace.system.domain.dto.InvestorCollectDTO;
import com.senlingspace.system.domain.dto.InvestorShareDTO;
import com.senlingspace.system.domain.dto.OrderDto;
import com.senlingspace.system.domain.query.senling.QueryOrderExport;
import com.senlingspace.system.domain.vo.*;
import com.senlingspace.system.service.ISlkjOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 订单Controller
 *
 * @author SenlingSpace
 * @date 2024-07-01
 */
@RestController
@RequestMapping("/system/order")
public class SlkjOrderController extends BaseController {


    @Autowired
    private ISlkjOrderService slkjOrderService;


    /**
     * 投资人收益汇总 echar图数据
     *
     * @param investorCollectVo 订单
     * @return 订单集合
     */
    @PreAuthorize("@ss.hasPermi('system:order:Investor')")
    @GetMapping("/investor")
    public AjaxResult getInvestorTotalAmount(@Validated InvestorCollectVo investorCollectVo) {
        InvestorPersionVO investorTotalAmount = slkjOrderService.getInvestorTotalAmount(investorCollectVo);
        return success(investorTotalAmount);
    }


    /**
     * 投资方日分账汇总
     *
     * @param investorShareVo 订单
     * @return 订单集合
     */
    @PreAuthorize("@ss.hasPermi('system:order:listCollect')")
    @GetMapping("/listCollect")
    public TableDataInfo getInvestorCollect(InvestorCollectVo investorShareVo) {
        return getDataTable(slkjOrderService.getInvestorCollect(investorShareVo));
    }

    /**
     * 导出投资方日分账汇总
     */
    @PreAuthorize("@ss.hasPermi('system:listCollect:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCollect")
    public void exportCollect(HttpServletResponse response, InvestorCollectVo investorShareVo) {
        List<InvestorCollectDTO> list = slkjOrderService.getInvestorCollect(investorShareVo);
        ExcelUtil<InvestorCollectDTO> util = new ExcelUtil<InvestorCollectDTO>(InvestorCollectDTO.class);
        util.exportExcel(response, list, "导出投资方日分账汇总表");
    }


    /**
     * 投资方日分账明细表
     *
     * @param investorShareVo 订单
     * @return 订单集合
     */
    @PreAuthorize("@ss.hasPermi('system:order:listInvestor')")
    @GetMapping("/listInvestor")
    public TableDataInfo getInvestorProfitShare(InvestorShareVo investorShareVo) {
        return getDataTable(slkjOrderService.getInvestorProfitShare(investorShareVo));
    }

    /**
     * 导出投资方日分账明细表
     */
    @PreAuthorize("@ss.hasPermi('system:listInvestor:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportInvestor")
    public void exportInvestor(HttpServletResponse response, InvestorShareVo investorShareVo) {
        List<InvestorShareDTO> list = slkjOrderService.getInvestorProfitShare(investorShareVo);
        ExcelUtil<InvestorShareDTO> util = new ExcelUtil<InvestorShareDTO>(InvestorShareDTO.class);
        util.exportExcel(response, list, "导出投资方日分账明细表");
    }

    /**
     * 查询订单列表和详情
     */
    @PreAuthorize("@ss.hasPermi('system:order:detail')")
    @GetMapping("/detail")
    public TableDataInfo listDetail(OrderDto lkjOrder) {
        PageInfo<OrderDetailVo> list = slkjOrderService.listDetail(lkjOrder);
        return getDataTable(list);
    }

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlkjOrder slkjOrder) {
        startPage();
        List<SlkjOrder> list = slkjOrderService.selectSlkjOrderList(slkjOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QueryOrderExport queryOrderExport) {
        List<OrderListExportVo> orderList = slkjOrderService.selectOrderListExportList(queryOrderExport);
        ExcelUtil<OrderListExportVo> util = new ExcelUtil<OrderListExportVo>(OrderListExportVo.class);
        util.exportExcel(response, orderList, "订单数据");
        System.out.println(util);
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return success(slkjOrderService.selectSlkjOrderByOrderId(orderId));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(slkjOrderService.deleteSlkjOrderByOrderIds(orderIds));
    }

    /**
     * 查询门店日收入明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:daily:list')")
    @GetMapping("/daily")
    public TableDataInfo details(Date startTime, Date endTime, Long shopId) {
        List<DetailsVo> list = slkjOrderService.details(startTime, endTime, shopId);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:daily:export')")
    @Log(title = "门店日收入明细", businessType = BusinessType.EXPORT)
    @PostMapping("/dailyList")
    public void detailsExport(HttpServletResponse response, Date startTime, Date endTime, Long shopId) {
        List<DetailsVo> list = slkjOrderService.details(startTime, endTime, shopId);
        ExcelUtil<DetailsVo> util = new ExcelUtil<DetailsVo>(DetailsVo.class);
        util.exportExcel(response, list, "门店日收入明细数据");
    }


    /**
     * 到付商品订单列表
     *
     * @param request
     * @return {@link TableDataInfo }
     */
    @PreAuthorize("@ss.hasPermi('system:order:paymentuponreceipt')")
    @GetMapping(path = "/paymentUponReceipt")
    public TableDataInfo paymentUponReceipt(PaymentUponReceiptRequest request) {
        PageInfo<PaymentUponReceiptDTO> result = slkjOrderService.paymentUponReceipt(request, true);
        return super.getDataTable(result);
    }

    /**
     * 导出Excel 到付商品订单列表
     *
     * @param request
     * @return {@link TableDataInfo }
     */
    @Log(title = "到付商品订单列表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:order:exportpaymentuponreceipt')")
    @PostMapping(path = "/exportPaymentUponReceipt")
    public void exportPaymentUponReceipt(HttpServletResponse response, PaymentUponReceiptRequest request) {
        PageInfo<PaymentUponReceiptDTO> result = slkjOrderService.paymentUponReceipt(request, false);
        logger.info("导出到付商品订单列表，数据结果：{}", JSON.toJSONString(result));
        ExcelUtil<PaymentUponReceiptDTO> util = new ExcelUtil<>(PaymentUponReceiptDTO.class);
        util.exportExcel(response, result.getList(), "到付商品订单列表");
    }

    /**
     * 到付商品订单详情
     *
     * @param orderId
     * @return {@link AjaxResult }
     */
    @PreAuthorize("@ss.hasPermi('system:order:paymentuponreceiptdetail')")
    @GetMapping(path = "/paymentUponReceipt/{orderId}")
    public AjaxResult paymentUponReceiptDetail(@PathVariable Long orderId) {
        return slkjOrderService.paymentUponReceiptDetail(orderId);
    }

    /**
     * 更新订单发货状态
     *
     * @param request
     * @return {@link AjaxResult }
     */
    @PreAuthorize("@ss.hasPermi('system:order:updateorderdeliverystatus')")
    @PutMapping(path = "/updateOrderDeliveryStatus")
    public AjaxResult updateOrderDeliveryStatus(@RequestBody OrderDeliveryStatusRequest request) {
        return slkjOrderService.updateOrderDeliveryStatus(request);
    }

}